<template>
  <div>
    <el-container>
      <el-header
        height="100%"
        width="100%"
        align="center"
      >
        <el-tooltip
          class="item"
          effect="dark"
          content="点击进入首页"
          placement="bottom"
        >
          <router-link to="/">
            <img
              src="../assets/timg.jpg"
              style="height: 170px;width: 210px; margin-top: 10px;"
            >
          </router-link>
        </el-tooltip>
      </el-header>
      <el-main>
        <el-tabs
          v-model="activeName"
          type="card"
          @tab-click="handleClick"
        >
          <el-tab-pane
            label="注册"
            name="second"
          >
            <el-form
              :model="registerForm"
              status-icon
              :rules="registerRules"
              ref="registerForm"
              label-width="100px"
              class="demo-ruleForm"
              style="height: 400px;width: 400px;"
            >
              <el-form-item
                label="用户名"
                prop="username"
              >
                <el-input
                  type="text"
                  v-model="registerForm.username"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="密码"
                prop="password"
              >
                <el-input
                  type="password"
                  v-model="registerForm.password"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="邮箱"
                prop="email"
              >
                <el-input
                  type="email"
                  v-model="registerForm.email"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  @click="submitForm('registerForm')"
                >注册</el-button>
                <el-button @click="resetForm('registerForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane
            label="登录"
            name="first"
          >
            <el-form
              :model="loginForm"
              status-icon
              :rules="loginRules"
              ref="loginForm"
              label-width="100px"
              class="demo-ruleForm"
              style="height: 400px;width: 400px;"
            >
              <el-form-item
                label="用户名"
                prop="username"
              >
                <el-input
                  type="text"
                  maxlength="12"
                  v-model="loginForm.username"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="密码"
                prop="password"
              >
                <el-input
                  type="password"
                  v-model="loginForm.password"
                  autocomplete="off"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  @click="submitForm('loginForm')"
                >登录</el-button>
                <el-button @click="resetForm('loginForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-main>
    </el-container>
  </div>
</template>
<script>

export default {
  
  name: "Login",
  data() {
    //密码校验
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入内容"));
      }else{
        callback();
      }
    };
    //用户校验
    var validateName = (rule, value, callback) => {
      //let reg = /^\w{6,20}$/;
      if (value === "") {
        callback(new Error("请输入内容"));
       } 
      //else if (!reg.test(value)) {
      //   callback(new Error("请输入6到20个字符"));
      // } 
      else {
        callback();
      }
    };

    return {
      isDisable: "false",
      //用户静态数据
      user: {
        username: 2017122124,
        password: "20200628Test"
      },
      activeName: "second",
      loginForm: {
        password: "123",
        username: "lkxx"
      },
      loginRules: {
        password: [{ validator: validatePass, trigger: "blur" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          //{ min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        ]
      },
      registerForm: {
        password: "",
        username: "",
        email: ""
      },
      registerRules: {
        password: [{ validator: validatePass, trigger: "blur" }],
        // username: [
        //   { required: false, message: "请输入用户名", trigger: "blur" }
        //   //{ min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" }
        // ]
       username: [{ validator: validateName, trigger: "blur" }],
        email: [
          { required: true, message: "请输入邮箱地址", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ]
      }
    };
  },
  methods: {
    open(message) {
      this.$alert(message, {
        confirmButtonText: "返回"
      });
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    submitForm(formName) {
      const _this = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 提交逻辑
          let form = this.$refs[formName].$el;
          if (formName == "loginForm") {
            //表单格式提交
            let formData = new FormData(form);
            formData.append("username", this.loginForm.username);
            formData.append("password", this.loginForm.password);
            this.$axios.post("/user/login", this.loginForm).then(res => {
              console.log(res.data)
              const token =  res.data.token;
              _this.$store.commit("SET_TOKEN", token);
              _this.$store.commit("SET_USERINFO", res.data.userInfo);
              _this.$router.push("/");
              /* console.log(token);
                                console.log(res.data.data);*/
            });
          } else {
            //Json格式提交;
            this.$axios.post("/user/register", this.registerForm).then(res => {
              const token =  res.data.token;
              _this.$store.commit("SET_TOKEN", token);
              _this.$store.commit("SET_USERINFO", res.data.userInfo);
              _this.$router.push("/");
              console.log(this.registerForm.username+"||"+this.registerForm.password);
              console.log(res.data);
              alert(res.data.msg);
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.$notify({
      title: "看这里：",
      message: "Hello~ 快体验一下吧",
      duration: 2000
    });
  }
};
</script>

<style scoped>
.el-tabs {
  margin: 0 auto;
  max-width: 400px;
}

.el-main {
  /*background-color: #E9EEF3;*/
  color: #333;
  text-align: center;
  line-height: 20px;
  width: 100%;
}

.el-header,
.el-footer {
  /* background-color: #b3c0d1; */
  background-color: white;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}
</style>